(function(){

'use strict';

angular.module('controllers.faultLocation.pbossOm')
  .controller('serviceOpenController', serviceOpenController);

function serviceOpenController ($scope, pbossOmServer,$filter,COREDATA,CoreService,CoreLoading) {
	$scope.flag = false;
	$scope.shoumun = 1;
	$scope.provinces = COREDATA.PROVINCES;
	$scope.errorInfo = COREDATA.ERRORINFO; //日期错误信息
	$scope.translate = {
	  provinceCodes: [],
		workOrderTypeCode: [],
		workOrderStateCode:[],
		workOrderLevelCode:[],
		startTime:new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 31),
		endTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1)
	};

  $scope.levelList = [
	  {code:'01',interpretation:'一般级别'},{code:'02',interpretation:'较高级别'},{code:'03',interpretation:'最高级别'}
  ];
	$scope.stateList = [
		{code:'01',interpretation:'待处理'},{code:'02',interpretation:'正常业务等待中'},
		{code:'03',interpretation:'忽略'},{code:'04',interpretation:'异常等待'},
		{code:'05',interpretation:'取消开通'},{code:'06',interpretation:'正在处理'},
		{code:'07',interpretation:'已经处理完成'},{code:'08',interpretation:'等待同步'},
		{code:'09',interpretation:'同步计费'},{code:'10',interpretation:'发起同步失败'},
		{code:'11',interpretation:'已发送计费同步请求'},{code:'12',interpretation:'计费反馈同步失败'},
		{code:'13',interpretation:'结束'}
	];

	$scope.typeList = [
		{code:'01',interpretation:'一般级别'},{code:'02',interpretation:'较高级别'},{code:'03',interpretation:'最高级别'}
	];


	$scope.queryServiceOpen = function(){
		var paramsData = {};
		var provinceCodes;
		$scope.shoumun==1?$scope.shoumun=0:$scope.shoumun=1;
		provinceCodes = $scope.translate.provinceCodes.map(function(item){
		  return item.provinceCode;
		});
		paramsData.provinceCodes = provinceCodes;
		$scope.startTime = $filter('date')($scope.translate.startTime,'yyyyMMdd');
		$scope.endTime = $filter('date')($scope.translate.endTime,'yyyyMMdd');
		paramsData.startTime = $filter('date')($scope.translate.startTime,'yyyy-MM-dd');
		paramsData.endTime = $filter('date')($scope.translate.endTime,'yyyy-MM-dd');
		if($scope.shoumun === 0){
			CoreLoading.setState(true);
			exportExcel();
			pbossOmServer.queryServiceOpen(paramsData,queryServiceData);
		}
	};

	$scope.resetValue = function(){
		$scope.translate = {
			provinceCodes: [],
			workOrderTypeCode: [],
			workOrderStateCode:[],
			workOrderLevelCode:[],
			startTime:new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 31),
			endTime: new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate() - 1)
		};
	};

	var businessEchart = echarts.init(document.getElementById('pbossRate'));
	
  function queryServiceData(res) {
  	  CoreLoading.setState(false);

	  if(res.state === '10000' && res.data.pageData.length > 0 ){
		  $scope.flag = true;
		  $scope.falgObj = true;
		  $scope.serviceData = res.data.pageData;

		  var provinceNameArr = [];
		  var provinceValue = [];
		  angular.forEach($scope.serviceData,function(itemData){
			  provinceNameArr.push(itemData.provinceName);
			  provinceValue.push(itemData.indentCount);
		  });

		  var itemStyle =  {
			  normal: { // 系列级个性化，横向渐变填充
				  borderRadius: 5,
				  label: {
					  show: true,
					  textStyle: {
						  fontSize: '10',
						  fontFamily: '微软雅黑',
						  fontWeight: 'bold'
					  }
				  }
			  }
		  };
		  var option = {
			  title: {
				  text: '按地市统计工单数量'+$scope.startTime+'-'+$scope.endTime,
				  x: 'center'
			  },
			  tooltip: {
				  trigger: 'axis',
				  axisPointer: {
					  type: 'shadow'
				  }
			  },
			  legend: {
				  orient: 'vertical',
				  x: 'right',
				  y: 'center',
				  data: ['工单数']
			  },
			  calculable: true,
			  grid: {
				  x2: '13%'
			  },
			  xAxis: [
				  {
					  type: 'category',
					  data: provinceNameArr
				  }
			  ],
			  yAxis: [
				  {
					  type: 'value'
				  }
			  ],
			  series: [
				  {
					  name: '工单数',
					  type: 'bar',
					  barMaxWidth:'50',
					  itemStyle: itemStyle,
					  data: provinceValue
				  }
			  ]
		  };
		  businessEchart.clear();
		  businessEchart.setOption(option);
	  }

  }
  function exportExcel(){
  	$scope.outData = {
  		startTime: $scope.startTime,
  		endTime: $scope.endTime,
  		title: $scope.startTime+ '至'+$scope.endTime+'服务数据开通报表统计'
  	};
  	$scope.exportUrl = COREDATA.FORMPREFIX + 'OMdisplay/PBossOM/businessReportAnalysis/serviceOpen/export'; //表导出的请求地址

  }
  $scope.queryServiceOpen();
}

})();